<?php 
	include "../service/util/authenticateSession.php";
	$url = ($_SERVER['REQUEST_URI']);
	if (!preg_match('/^id=[1-9]{1,}[0-1]*$/', substr($url, strrpos($url, "?") + 1, strlen($url))))
		echo "<script>window.location.href= 'index.php';</script>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <?php 
		include("import.php");
		include_once("../service/receiptService.php");
		include_once("../service/ProductService.php");
		
		$receiptId = $_GET["id"];
		$result = findReceiptBy($receiptId);
		if (ConnectionManager::getInstance()->getNumRows() == 0)
			echo "<script>window.location.href='index.php';</script>";
			
		$row = ConnectionManager::getInstance()->fetch($result);
	?>
	<title>Hóa Đơn - <?php echo $row["RECEIPT_CODE"]; ?></title>
	
</head>
<body>
<?php include "header.php"; ?>
<form id="form">
<!-- Container -->
<div id="container">
	<div class="shell">
		<div id="main">
			<div class="cl">&nbsp;</div>
			<!-- Content -->
			<div id="content">        
				<!-- Box -->
				<div class="box">
					<!-- Box Head -->
					<div class="box-head">
						<h2>Thông Tin Tài Khoản</h2>
					</div>
					<!-- End Box Head -->
						<!-- Form -->
						<div class="form">
                        	<?php
								
								
                            ?>
                            <p>
                                <label>Mã Hóa Đơn</label>
                                <input type="text" class="field size1" value="<?php echo $row["RECEIPT_CODE"]; ?>" disabled="disabled"/>
                            </p>
                            <p>
                                <label>Ngày</label>
                                <input type="text" class="field size1" value="<?php echo date_format(date_create($row["RECEIPT_DATE"]), "d/m/Y"); ?>" disabled="disabled"/>
                            </p>
                            <p>
                                <label>Trạng Thái</label>
                                <select class="field size1" disabled="disabled">
                                	<?php
										$statuses = array(1 => "Có Sẵn", 
													   2 => "Hết Hàng",
													   3 => "Disable",
													   4 => "Đang Về");
                                    	while($status = current($statuses)){
											if(key($statuses) == $row["RECEIPT_STATUS"]){
									?>
	                                    		<option value="<?php echo key($statuses); ?>" selected="selected"><?php echo($status);?></option>
                                    <?php
											} else {                                    
									?>    
	                                    		<option value="<?php echo key($statuses); ?>"><?php echo($status);?></option>
                                    <?php
											}
											next($statuses);
										}
                                    ?>
                                </select>
                            </p>
                            <p>
                                <label>Ghi Chú</label>
                                <input type="text" class="field size1" value="<?php echo $row["RECEIPT_NOTE"]; ?>" disabled="disabled"/>
                            </p>	
                            <p>
                                <label>Địa Chỉ</label>
                                <input type="text" class="field size1" value="<?php echo $row["RECEIPT_ADD"]; ?>" disabled="disabled"/>
                            </p>
                            <p>
                                <label>Số Điện Thoại</label>
                                <input type="text" class="field size1" value="<?php echo $row["RECEIPT_PHONE"]; ?>" disabled="disabled"/>
                            </p>
                            <p>
                                <label>Người Nhận</label>
                                <input type="text" class="field size1" value="<?php echo $row["RECEIPT_RECEIVER"]; ?>" disabled="disabled"/>
                            </p>						
						</div>
						<!-- End Form -->
						
					<div class="box-head">
						<h2 class="left">Danh Sách Sản Phẩm</h2> 
					</div>
					<!-- End Box -->
					
					<!-- Table -->
					<div class="table">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <th>STT</th>
                                <th>Mã Sản Phẩm</th>
                                <th>Tên</th>
                                <th>Giá</th>
                                <th>Giá Xuất Kho</th>
                                <th>Số Lượng</th>
                                <th>Trạng Thái</th>
                            </tr>
                            <?php
                            	$result = findProductsBy($receiptId);
								$count = 0;
								while($row = ConnectionManager::getInstance()->fetch($result)){
									$count++;
							?>
                            <tr>
                            	<td><?php echo $count; ?></td>
                                <td><?php echo $row["PRO_CODE"]; ?></td>
                                <td><?php echo $row["PRO_NAME_VN"]." - ".$row["PRO_NAME_EN"]; ?></td>
                                <td><?php echo number_format($row["PRO_PRICE"], 0, ",", "."); ?></td>
                                <td><?php echo number_format(substr($row["ITEM"], 0, strrpos($row["ITEM"], '/')), 0, ",", "."); ?></td>
                                <td><?php echo substr($row["ITEM"], strrpos($row["ITEM"], '/') + 1, strlen($row["ITEM"])); ?></td>
                                <td>
									<?php 
										$status = $row["PRO_STATUS"];
										if($status == 1)
											echo "Còn hàng";
										else if($status == 2)
											echo "Tạm hết hàng";
										else if($status == 3)
											echo "Ngừng bán";
										else if($status == 4)
											echo "Sắp về";
									?>
								</td>
                            </tr>
                            <?php
								}
                            ?>
                        </table>
					</div>
                    <!-- End Table -->
                    
                    <!-- Form Buttons -->
						<div class="buttons">
                            <input type="button" class="button" value="Trở Về" onclick="window.location = 'receipt.php';" />
						</div>
					<!-- End Form Buttons -->
				</div>
                <!-- End Box -->
			</div>
			<!-- End Content -->
			<div class="cl">&nbsp;</div>			
		</div>
		<!-- Main -->
	</div>
</div>
<!-- End Container -->
</form>
<?php 
	include "footer.php";
	ConnectionManager::getInstance()->closeConnection(); 
?>
</body>
</html>
